<?php

namespace App\Http\Controllers\Api\User;

use App\Http\Controllers\Controller;
use App\Services\PublishService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class PublishController extends Controller
{
    public function list(Request $request)
    {
        $rule        = [
            "user_id" => "required|integer|exists:user,id",
            "status"  => "required|integer|min:0"
        ];
        $input       = $request->input();
        $validateRet = Validator::make($input, $rule);
        if ($validateRet->fails()) {
            return result(1, $validateRet->errors()->first());
        }

        $page  = $request->input('page', 1);
        $limit = $request->input('limit', 10);
        $sortField  = $request->input('sort', 'updated_at');
        $orderRule = $request->input('order', 'desc');

        $recruitTableColumns = ['id', 'title', 'city', 'status', 'updated_at'];
        $where[] = ['status', $input['status']];

        try {
            $data = (new PublishService())->getPublishRecruitListByUser($input, $page, $limit, $sortField, $orderRule, $recruitTableColumns, $where);
        } catch (\Exception $e) {
            return result(1, $e->getMessage());
        }

        return result(0, 'ok', $data);
    }

    public function editRecruitStatus(Request $request)
    {
        $rule        = [
            "id"          => "required|integer|exists:recruit,id",
            "user_id"     => "required|integer|exists:user,id",
            "action_type" => "required"
        ];
        $input       = $request->input();
        $validateRet = Validator::make($input, $rule);
        if ($validateRet->fails()) {
            return result(1, $validateRet->errors()->first());
        }

        try {
            (new PublishService())->editRecruitStatus($input);
        } catch (\Exception $e) {
            return result(1, $e->getMessage());
        }

        return result(0, 'ok');
    }
}
